#include "string.h"
#include "math.h"
#include "crc8.h"
#include "delay.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"
#include "queue.h"

#include "myTask.h"

#include "bsp_timer.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "bsp_adc.h"
#include "bsp_can1.h"

#include "motorstepper.h"

#include "switch.h"
#include "RS7_ABSencoder.h"
#include "RGB_Belt.h"
#include "armmotor.h"
#include "stepper.h"
#include "stepperctrl.h"
#include "RelayMotor.h"

#include "armaction.h"
#include "armeventctrl.h"
#include "usarttransmitter.h"
#include "cantransmitter.h"
#include "communication.h"
#include "servercomm.h"
#include "manage.h"
#include "MasterAppReset.h"


#define SENDDATATIME    4


extern QueueHandle_t Usart1Debug_Queue;
extern QueueHandle_t ServerCmd_Queue;	//串口2接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can1_Tx_Queue;		//CAN信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can1_Rx_Queue;
extern QueueHandle_t Usart6_Rx_Queue;	//串口6信息队列句柄，用于中断和任务通讯


extern TaskHandle_t StartTask_Handler;
extern TaskHandle_t Step_Motor_Task_Handler;
extern TaskHandle_t LightLimit_Task_Handler;
extern TaskHandle_t ArmEvent_Task_Handler;
extern TaskHandle_t Comunication_Task_Handler;
extern TaskHandle_t Sensor_Task_Handler;
extern TaskHandle_t Manage_Task_Handler;

//LightLimit_task
void LightLimit_task(void *pvParameters)
{
	while(1)
	{
		ArmMotorLimitCtrl();     //光电检测
		if(!Get_ArmReset())
			reset2isp();
		
		hard_watchdog_feed();//硬件喂狗		
        vTaskDelay(10);
	}
}

//step_motor_task
void step_motor_task(void *pvParameters)
{
	while(1)
	{
	    ArmMotorCtrl();
        vTaskDelay(10);
	}
}

//Armevent_task
void Armevent_task(void *pvParameters)
{
	while(1)
	{
		ArmEventHandle();
		green_led_blink();
		vTaskDelay(10);
	}
}

void comunication_task(void *pvParameters)
{
	static uint8_t send_cnt = 0;
	static uint8_t usart1queuebuf[USART1_RX_SIZE]      = {0};	
	static uint8_t serverqueuebuf[USART2_MAX_RECV_LEN] = {0};
	static uint8_t usart6queuebuf[USART6_MAX_RECV_LEN] = {0};
    static CanRxMsg  can1msg;
	static uint8_t server_uploda_cnt = 5;
	while(1)
	{
		if(xQueueReceive(Usart1Debug_Queue,usart1queuebuf,10)==pdTRUE){
			UsartProcess(usart1queuebuf,USART1_RX_SIZE);//解析处理串口1调试数据
            CarHangar_Param_Parse(usart1queuebuf);
			IapJump2Bootloader(serverqueuebuf);
		}
		
		if(xQueueReceive(ServerCmd_Queue,serverqueuebuf,10)==pdTRUE){
			Cjson_Prase((const char*)serverqueuebuf);
			IapJump2Bootloader(serverqueuebuf);
		}
        
        if(xQueueReceive(Can1_Rx_Queue,&can1msg,5)==pdTRUE){
            Can1Process(can1msg.StdId,can1msg.Data);
        }
        
		if(xQueueReceive(Usart6_Rx_Queue,usart6queuebuf,10)==pdTRUE){
			Rs7abs_fun.RS7ABS_Prase(usart6queuebuf,USART6_MAX_RECV_LEN);//解析绝对式编码器数据
			Rs7abs_fun.RS7ABS_Get_Abs();
		}
	
		if(Wait_Singal(&server_uploda_cnt,5))
		{
			send_cnt++;
			if(send_cnt==1){
				Sensor_Data_Packed1_Upload_JSON();//send to server
			}
			else if(send_cnt==2){
                send_cnt = 0;
				Sensor_Data_Packed2_Upload_JSON();//send to server
			}
		}
		vTaskDelay(100);
	}
}

void sensor_task(void *pvParameters)
{
	static uint8_t rs_flag = 1;
	static uint8_t cnt = 0;
	while(1)
	{
        Send_Can1_Data_By_Queue();
		rs_flag ? (Rs7abs_fun.RS7ABS_Data_Ask(1),rs_flag = 0):(Rs7abs_fun.RS7ABS_Data_Ask(2),rs_flag = 1);
		if(Wait_Singal(&cnt,5)){
            SwitchTest();
        }
		Usart2_Send();
		vTaskDelay(100);
	}
}

void manage_task(void *pvParameters)
{
	static uint8_t offline_time = 0;
	while(1)
	{
//		Key_Scan_Task();
		if(Wait_Singal(&offline_time,100)) {OffLine_Manage();};
		Hangar_State_Manage();
		Flash_Manage();
		vTaskDelay(50);
    }
}



